﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNetUniLodz.Models;

namespace DotNetUniLodz.Controllers
{
    public class CategoriesController : Controller
    {
        private DotNetULEntities db = new DotNetULEntities();

        //
        // GET: /Categories/

        public ActionResult Index()
        {
            var categories = db.Categories.Include(c => c.Category2).OrderBy(c => c.Category_name);
            return View(categories.ToList());
        }

        //
        // GET: /Categories/Details/5

        public ActionResult Details(string id)
        {
            Category category = db.Categories.FirstOrDefault(c => c.Category_name == id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Categories/Create

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            ViewBag.Category_parent = new SelectList(db.Categories, "Category_id", "Category_name");
            return View();
        }

        //
        // POST: /Categories/Create

        [Authorize(Roles = "Admin")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrWhiteSpace(category.Category_name)
                    && !db.Categories.Any(c => c.Category_name == category.Category_name))
                {
                    db.Categories.Add(category);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }

            ViewBag.Category_parent = new SelectList(db.Categories, "Category_id", "Category_name", category.Category_parent);
            return View(category);
        }

        //
        // GET: /Categories/Edit/5

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(short id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            ViewBag.Category_parent = new SelectList(db.Categories, "Category_id", "Category_name", category.Category_parent);
            return View(category);
        }

        //
        // POST: /Categories/Edit/5

        [Authorize(Roles = "Admin")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrWhiteSpace(category.Category_name) && !db.Categories.Any(c => c.Category_name == category.Category_name))
                {
                    db.Entry(category).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.Category_parent = new SelectList(db.Categories, "Category_id", "Category_name", category.Category_parent);
            return View(category);
        }

        //
        // GET: /Categories/Delete/5

        [Authorize(Roles = "Admin")]
        public ActionResult Delete(short id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Categories/Delete/5

        [Authorize(Roles = "Admin")]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(short id)
        {
            Category category = db.Categories.Find(id);
            db.Categories.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}